package com.kmsoft.gateway.application.impl;

import com.kmsoft.gateway.application.GatewayConfigApplicationService;
import com.kmsoft.gateway.domain.GatewayConfigDomain;
import com.kmsoft.gateway.repository.GatewayConfigRepository;
import lombok.extern.slf4j.Slf4j;
import org.springframework.cloud.gateway.event.RefreshRoutesEvent;
import org.springframework.cloud.gateway.route.RouteDefinitionWriter;
import org.springframework.context.ApplicationEventPublisher;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;

@Slf4j
@Service
public class GatewayConfigApplicationServiceImpl implements GatewayConfigApplicationService {

    @Resource
    private GatewayConfigRepository gatewayConfigRepository;
    @Resource
    private RouteDefinitionWriter routeDefinitionWriter;
    @Resource
    private ApplicationEventPublisher publisher;


    @Override
    public void refreshRoute() {
        for (GatewayConfigDomain domain : gatewayConfigRepository.list()) {
            domain.loadRoute(routeDefinitionWriter);
        }
        publisher.publishEvent(new RefreshRoutesEvent(this));
        log.info("==============route refresh finish");
    }
}
